﻿using BootstrapBlazor.Components;
using CardMaker.Library.Models;

namespace CardMaker.Library.Services;

public class CreateCardService : ICreateCardService {
    public ICardImageToUrlService _cardImageToUrlService;
    public ICardTemplateStorage _cardTemplateStorage;
    public ISideImageStorage _sideImageStorage;
    public IFrameImageStorage _frameImageStorage;

    public CreateCardService(ICardTemplateStorage cardTemplateStorage,
        ISideImageStorage sideImageStorage,
        ICardImageToUrlService cardImageToUrlService, IFrameImageStorage frameImageStorage) {
        _cardTemplateStorage = cardTemplateStorage;
        _sideImageStorage = sideImageStorage;
        _cardImageToUrlService = cardImageToUrlService;
        _frameImageStorage = frameImageStorage;
    }

    public Task<string> GetMainImageAsync() {
        throw new NotImplementedException();
    }

    public async Task<string> GetFrameImageAsync(CardTemplate cardTemplate) {
        byte[] frameBytes = null;
        frameBytes = await _frameImageStorage.GetFrameImagesAsync(cardTemplate);
        return await _cardImageToUrlService.GenerateImageUrl(frameBytes);
    }

    public Task<List<TextFrame>> GetTextFramesAsync(CardTemplate cardTemplate) {
        throw new NotImplementedException();
    }

    public async Task<IList<JSideImage>> GetJSideImagesUrlAsync(
        CardTemplate cardTemplate) {
        
        var sideImages =
            await _sideImageStorage.GetSideImagesAsync(cardTemplate);
        List<JSideImage> jSideImages = new List<JSideImage>();
        try {
            foreach (var tempSideImage in sideImages) {
                if (tempSideImage.SideImageKeXuan != null)
                    jSideImages.Add(new JSideImage
                    {
                        TemplateId = tempSideImage.TemplateId,
                        SideImageId = tempSideImage.SideImageId,
                        SideImageName = tempSideImage.SideImageName,
                        //todo
                        Scale = double.Parse(tempSideImage.Scale),
                        SideImagePosition =
                            await GetPosition(tempSideImage.SideImagePosition),
                        NumberOfKeXuanSideImage =
                            tempSideImage.SideImageKeXuan.Count,
                        /*SideImageKeXuan = tempSideImage.SideImageKeXuan,*/
                        SideImageKeXuan =
                            (List<string>)await CheckTheKeXuan(tempSideImage
                                .SideImageKeXuan),
                        SideKeXuanImageUrls =
                             await ChangeToUrlAsync(tempSideImage
                                .SideKeXuanImageBytes)
                    });
            }

            /*await ChangeToJ(sideImages);*/

        } catch (Exception e) {
            System.Console.WriteLine(e);
            throw;
        }

        return jSideImages;
    }

    public async Task RevokeAsync() {
        //todo
        /*foreach (var temp in _sideImageUrlList)
            await _cardImageToUrlService.RevokeUrl(temp);*/
    }

    private async Task<List<string>>
        ChangeToUrlAsync(IList<byte[]> bytesList) {
        var sideImageUrlList = new List<string>();
        sideImageUrlList.Clear();
        for (int i = 0; i < bytesList.Count; i++) {
            sideImageUrlList.Add(
                await _cardImageToUrlService.GenerateImageUrl(bytesList[i]));
        }
        return sideImageUrlList;
    }
    /*private async Task
        ChangeToJ(IList<SideImage> sideImageList)//无用，修bug的一个尝试
    {
        for (int j = 0; j < sideImageList.Count; j++) {
            if (sideImageList[j].SideImageKeXuan != null) {
                for (int i = 0; i < sideImageList[j].SideImageKeXuan.Count; i++)
                {
                    jSideImages[j].SideKeXuanImageUrls.Add(
                        await _cardImageToUrlService.GenerateImageUrl(sideImageList[j].SideKeXuanImageBytes[i]));
                }
            }
        }
    }*/

    private async Task<IList<string>> CheckTheKeXuan(IList<string> kexuanList) {
        var keXuanList = new List<string>();
        await Task.Run(() => {
            if (keXuanList.Count != 0)
                keXuanList.Clear();
            foreach (var item in kexuanList)
                keXuanList.Add(item);
            return Task.CompletedTask;
        });
        return keXuanList;
    }

    private async Task<double[]> GetPosition(string position) {
        double x = 0, y = 0;
        //todo 有待解析的位置信息
        double[] ps = {x, y};
        return ps;
    }
}